//
//  ArticleMsg.m
//
//  Created by Heige  on 16/5/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "ArticleMsg.h"
#import "ArticleParent.h"


NSString *const kArticleMsgCaTime = @"caTime";
NSString *const kArticleMsgParent = @"Parent";
NSString *const kArticleMsgCaId = @"caId";
NSString *const kArticleMsgCaContent = @"caContent";
NSString *const kArticleMsgPId = @"pId";
NSString *const kArticleMsgCaVoice = @"caVoice";
NSString *const kArticleMsgCaViewNum = @"caViewNum";
NSString *const kArticleMsgCaFavNum = @"caFavNum";
NSString *const kArticleMsgCaCommentNum = @"caCommentNum";
NSString *const kArticleMsgCaTitle = @"caTitle";


@interface ArticleMsg ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ArticleMsg

@synthesize caTime = _caTime;
@synthesize parent = _parent;
@synthesize caId = _caId;
@synthesize caContent = _caContent;
@synthesize pId = _pId;
@synthesize caVoice = _caVoice;
@synthesize caViewNum = _caViewNum;
@synthesize caFavNum = _caFavNum;
@synthesize caCommentNum = _caCommentNum;
@synthesize caTitle = _caTitle;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.caTime = [self objectOrNilForKey:kArticleMsgCaTime fromDictionary:dict];
            self.parent = [ArticleParent modelObjectWithDictionary:[dict objectForKey:kArticleMsgParent]];
            self.caId = [[self objectOrNilForKey:kArticleMsgCaId fromDictionary:dict] doubleValue];
            self.caContent = [self objectOrNilForKey:kArticleMsgCaContent fromDictionary:dict];
            self.pId = [self objectOrNilForKey:kArticleMsgPId fromDictionary:dict];
            self.caVoice = [self objectOrNilForKey:kArticleMsgCaVoice fromDictionary:dict];
            self.caViewNum = [[self objectOrNilForKey:kArticleMsgCaViewNum fromDictionary:dict] doubleValue];
            self.caFavNum = [[self objectOrNilForKey:kArticleMsgCaFavNum fromDictionary:dict] doubleValue];
            self.caCommentNum = [[self objectOrNilForKey:kArticleMsgCaCommentNum fromDictionary:dict] doubleValue];
            self.caTitle = [self objectOrNilForKey:kArticleMsgCaTitle fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.caTime forKey:kArticleMsgCaTime];
    [mutableDict setValue:[self.parent dictionaryRepresentation] forKey:kArticleMsgParent];
    [mutableDict setValue:[NSNumber numberWithDouble:self.caId] forKey:kArticleMsgCaId];
    [mutableDict setValue:self.caContent forKey:kArticleMsgCaContent];
    [mutableDict setValue:self.pId forKey:kArticleMsgPId];
    [mutableDict setValue:self.caVoice forKey:kArticleMsgCaVoice];
    [mutableDict setValue:[NSNumber numberWithDouble:self.caViewNum] forKey:kArticleMsgCaViewNum];
    [mutableDict setValue:[NSNumber numberWithDouble:self.caFavNum] forKey:kArticleMsgCaFavNum];
    [mutableDict setValue:[NSNumber numberWithDouble:self.caCommentNum] forKey:kArticleMsgCaCommentNum];
    [mutableDict setValue:self.caTitle forKey:kArticleMsgCaTitle];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.caTime = [aDecoder decodeObjectForKey:kArticleMsgCaTime];
    self.parent = [aDecoder decodeObjectForKey:kArticleMsgParent];
    self.caId = [aDecoder decodeDoubleForKey:kArticleMsgCaId];
    self.caContent = [aDecoder decodeObjectForKey:kArticleMsgCaContent];
    self.pId = [aDecoder decodeObjectForKey:kArticleMsgPId];
    self.caVoice = [aDecoder decodeObjectForKey:kArticleMsgCaVoice];
    self.caViewNum = [aDecoder decodeDoubleForKey:kArticleMsgCaViewNum];
    self.caFavNum = [aDecoder decodeDoubleForKey:kArticleMsgCaFavNum];
    self.caCommentNum = [aDecoder decodeDoubleForKey:kArticleMsgCaCommentNum];
    self.caTitle = [aDecoder decodeObjectForKey:kArticleMsgCaTitle];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_caTime forKey:kArticleMsgCaTime];
    [aCoder encodeObject:_parent forKey:kArticleMsgParent];
    [aCoder encodeDouble:_caId forKey:kArticleMsgCaId];
    [aCoder encodeObject:_caContent forKey:kArticleMsgCaContent];
    [aCoder encodeObject:_pId forKey:kArticleMsgPId];
    [aCoder encodeObject:_caVoice forKey:kArticleMsgCaVoice];
    [aCoder encodeDouble:_caViewNum forKey:kArticleMsgCaViewNum];
    [aCoder encodeDouble:_caFavNum forKey:kArticleMsgCaFavNum];
    [aCoder encodeDouble:_caCommentNum forKey:kArticleMsgCaCommentNum];
    [aCoder encodeObject:_caTitle forKey:kArticleMsgCaTitle];
}

- (id)copyWithZone:(NSZone *)zone
{
    ArticleMsg *copy = [[ArticleMsg alloc] init];
    
    if (copy) {

        copy.caTime = [self.caTime copyWithZone:zone];
        copy.parent = [self.parent copyWithZone:zone];
        copy.caId = self.caId;
        copy.caContent = [self.caContent copyWithZone:zone];
        copy.pId = [self.pId copyWithZone:zone];
        copy.caVoice = [self.caVoice copyWithZone:zone];
        copy.caViewNum = self.caViewNum;
        copy.caFavNum = self.caFavNum;
        copy.caCommentNum = self.caCommentNum;
        copy.caTitle = [self.caTitle copyWithZone:zone];
    }
    
    return copy;
}


@end
